package com.example.duomeitijuhe.dao;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.example.duomeitijuhe.R;
import com.example.duomeitijuhe.others.ReducePicture;
import com.example.duomeitijuhe.others.RoundPicture;
import org.apache.http.protocol.RequestUserAgent;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

/**
 * Created by Michael on 2014/10/15.
 */
public class LoadPictures {

    public static String headIconUrl;

    private static Context context;

    private Bitmap headIconBitmap;
    private static ReducePicture reducePicture;
    private static RoundPicture roundPicture;

    public LoadPictures(){

    }

    public static Bitmap returnHeadBitmap(String url) {


        roundPicture = new RoundPicture();
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            conn.setConnectTimeout(2000);
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return roundPicture.toRoundBitmap(bitmap);
    }

    public static ArrayList<Bitmap> getContentPictures(String contentPicturesUrl){
        ArrayList<Bitmap> arrayList = new ArrayList<Bitmap>();
        try{
            JSONObject jsonObject = new JSONObject(contentPicturesUrl);
            JSONArray pictureObject = jsonObject.getJSONArray("pic_urls");
            for (int i= 0;i<pictureObject.length();i++){
                String url = pictureObject.getJSONObject(i).getString("thumbnail_pic");
                Bitmap bitmap = returnHeadBitmap(url);
                arrayList.add(bitmap);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return arrayList;
    }




    /* public Bitmap loadHeadPicture(String headUrl){


       reducePicture = new ReducePicture(context);
        roundPicture = new RoundPicture();
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        ImageRequest imageRequest = new ImageRequest(headUrl,
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {


                        if (bitmap == null){
                            Log.i("jdflak","kfajlsd");
                        }
                        //headIconBitmap = roundPicture.toRoundBitmap(bitmap);

                        headIconBitmap = bitmap;
                    }
                },
                0,0, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Bitmap bitmap = reducePicture.pictureToLess(R.drawable.ic_launcher,40);
                        //headIconBitmap = roundPicture.toRoundBitmap(bitmap);
                    }
                }
        );

        requestQueue.add(imageRequest);
        if (headIconBitmap == null){
            Log.i("LoadPictures","faksdfjlaksdjfasjdflk");
        }else {
            Log.i("kfjalsdkfja","dkafjsldkfjalkdfjalsdkfjalksdfjalkf");
        }
        return headIconBitmap;
    }*/


    }
